class ThingDao {
    constructor(pool) {
        this.pool = pool;
    }

    async getThingsByUserId(userId) {
        const sql = "select * from things where th_user_id=?";
        const [rows] = await this.pool.execute(sql, [userId]);
        return rows;
    }

    async addThing(content, userId) {
        const sql = "insert into things(th_id,th_content,th_isfinish,th_user_id) values(default,?,0,?)";
        return await this.pool.execute(sql, [content, userId]);
    }

    async deleteThing(id, userId) {
        const sql = "delete from things where th_id=? and th_user_id=?";
        return await this.pool.execute(sql, [id, userId]);
    }

    async updateThing(id, content) {
        const sql = "update things set th_content=? where th_id=?";
        return await this.pool.execute(sql, [content, id]);
    }

    async toggleThing(id) {
        const sql = "update things set th_isfinish = 1 where th_id=?";
        return await this.pool.execute(sql, [id]);
    }
}

module.exports = ThingDao;